% .lbl file:
%lbl_file = '../data/campus_with_force_4.lbl';
%lbl_file = '../data/0309expdata/protocol_without_speed.lbl';
%lbl_file = '../walk-n-stair-climb/1/pressure.lbl';
lbl_file = '../data/0309expdata/cont_walk_nospeed.lbl';
% .cla file (if not present, use 'classifiers' variable):
%tree_struct_file = 'tmp_tra.mat';
tree_struct_file = '';

%%
[testing_labels testing_data testing_header] = load_data_with_lbl(lbl_file); 
%%
testing_frames = framing_with_class(testing_labels, testing_data, testing_header);

% read structure of tree and associate frames to classifiers
%%
if exist('tree_struct_file', 'var') && ~isempty(tree_struct_file)
    load(tree_struct_file, '-mat');
end

% get feature set
testing_feature_set = {};
for fs = classifiers
    testing_feature_set = [testing_feature_set,fs.features];
end

%%
testing_assoc_classifiers = assoc_frame_to_classifier(testing_frames, testing_labels, classifiers);

%%
[ testing_feature_vals, testing_feature_set_struct, testing_feature_set_name ] = ...
    extract_all_feature( testing_data, testing_header, testing_frames, testing_feature_set );
[ classify_results, class_decision ] = ...
    classify( testing_feature_vals, testing_feature_set_name, testing_frames, testing_assoc_classifiers, testing_labels );

%%
[ result_matrix ] = get_stat_matrix(testing_frames, class_decision);
display(result_matrix);

for i=1:length(testing_assoc_classifiers)
    display(['Classifier ' num2str(i)]);
    for j=1:max(classify_results(:, i, 2))
        idx1 = find(classify_results(:, i, 2) == j);
        idx2 = find(classify_results(:, i, 1) == j);
        n_correct = length(intersect(idx1, idx2));
        n_fp = length(setdiff(idx2, idx1));
        n_fn = length(setdiff(idx1, idx2));
        class_name = '';
        for k=1:length(testing_assoc_classifiers{i}.classes{j})
            class_name = [class_name '+' testing_assoc_classifiers{i}.classes{j}{k}];
        end
        class_name = class_name(2:end);
        str = sprintf('Subclass %d[%s]: correct: %d, false_pos: %d, false_neg: %d. Accuracy: %f', j, class_name, n_correct, n_fp, n_fn, n_correct / length(idx1) * 100);
        display(['  * ' str '%']);
    end
    idx_true = find(classify_results(:, i, 2) > 0);
    idx_correct = find(classify_results(:, i, 2) == classify_results(:, i, 1));
    n_correct = length(intersect(idx_true, idx_correct));
    str = sprintf('Classifier Accuracy: %f', n_correct / length(idx_true) * 100);
    display(['  ' str '%']);
end

%%
draw_decision;
[combined_decision, speed_vector] = decision2label(testing_data, testing_frames, class_decision, testing_labels);
draw_decision_time(testing_labels, combined_decision);
srtgen(testing_labels, combined_decision, speed_vector, -2317.1, [lbl_file '.srt']);
